<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring @Primary 注解深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }
        .feature-card {
            transition: all 0.3s ease;
            backdrop-filter: blur(10px);
            background: rgba(255, 255, 255, 0.9);
        }
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .primary-badge {
            background: linear-gradient(45deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 2px 12px;
            border-radius: 20px;
            font-size: 0.875rem;
            font-weight: 500;
        }
        .section-title {
            position: relative;
            display: inline-block;
        }
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        pre code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: -0.5rem;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="text-center">
                <div class="mb-6">
                    <i class="fas fa-crown text-6xl opacity-80"></i>
                </div>
                <h1 class="text-5xl md:text-6xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                    Spring @Primary 注解
                </h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                    优雅解决 Bean 冲突，让依赖注入更加智能
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <span class="primary-badge"><i class="fas fa-star mr-2"></i>默认候选</span>
                    <span class="primary-badge"><i class="fas fa-check-circle mr-2"></i>简化配置</span>
                    <span class="primary-badge"><i class="fas fa-shield-alt mr-2"></i>避免冲突</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-16">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-10 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">@</span>Primary 注解是 Spring 框架中的一个强大工具，用于指定一个 Bean 作为默认的候选 Bean。当有多个候选 Bean 可供选择时，它能够避免 Bean 注入冲突，使得依赖注入变得更加简单和清晰。通过使用 @Primary 注解，开发者可以有效地控制 Bean 的注入行为，并简化配置流程。
            </p>
        </div>

        <!-- Visual Diagram -->
        <div class="bg-white rounded-2xl shadow-xl p-10 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-sitemap mr-3 text-purple-600"></i>工作原理图解
            </h2>
            <div class="mermaid">
                graph TD
                    A[Spring 容器] -->|扫描| B[多个候选 Bean]
                    B --> C{是否有 @Primary?}
                    C -->|是| D[选择 @Primary Bean]
                    C -->|否| E[抛出异常或需要 @Qualifier]
                    D --> F[成功注入]
                    E --> G[@Qualifier 指定]
                    G --> F
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#4ade80,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Use Cases -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="feature-card rounded-2xl shadow-lg p-8">
                <div class="text-purple-600 text-4xl mb-4">
                    <i class="fas fa-puzzle-piece"></i>
                </div>
                <h3 class="text-2xl font-bold mb-4 text-gray-800">使用场景</h3>
                <p class="text-gray-600 leading-relaxed">
                    在 Spring 的依赖注入过程中，如果有多个 Bean 实现了相同的接口或继承了相同的类，并且 Spring 需要自动注入这个接口或类的实例，就可能会遇到 Bean 冲突的问题。这时，@Primary 注解可以用来指定一个 Bean 作为默认注入的 Bean。
                </p>
            </div>

            <div class="feature-card rounded-2xl shadow-lg p-8">
                <div class="text-purple-600 text-4xl mb-4">
                    <i class="fas fa-magic"></i>
                </div>
                <h3 class="text-2xl font-bold mb-4 text-gray-800">主要功能</h3>
                <div class="space-y-4">
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800">解决 Bean 冲突</h4>
                            <p class="text-gray-600 text-sm">自动选择标记为 @Primary 的 Bean 作为默认注入对象</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800">简化配置</h4>
                            <p class="text-gray-600 text-sm">减少对 @Qualifier 注解的使用，使代码更简洁</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Code Example -->
        <div class="bg-white rounded-2xl shadow-xl p-10 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-code mr-3 text-purple-600"></i>示例代码
            </h2>
            
            <div class="mb-8">
                <p class="text-gray-600 mb-4">假设有两个 Bean 实现了相同的接口 <code class="bg-gray-100 px-2 py-1 rounded">GreetingService</code>：</p>
                
                <div class="code-block p-6 overflow-x-auto">
                    <pre><code class="text-gray-300">public interface GreetingService {
    String greet();
}

@Component
public class EnglishGreetingService implements GreetingService {
    @Override
    public String greet() {
        return "Hello";
    }
}

@Component
<span class="text-yellow-400">@Primary</span>
public class SpanishGreetingService implements GreetingService {
    @Override
    public String greet() {
        return "Hola";
    }
}</code></pre>
                </div>
            </div>

            <div class="bg-purple-50 rounded-xl p-6 mb-8">
                <p class="text-gray-700 leading-relaxed">
                    <i class="fas fa-lightbulb text-purple-600 mr-2"></i>
                    在上述示例中，<code class="bg-white px-2 py-1 rounded">SpanishGreetingService</code> 使用了 @Primary 注解，因此它将成为 <code class="bg-white px-2 py-1 rounded">GreetingService</code> 接口的默认实现。即使没有使用 @Qualifier 指定 Bean，Spring 也会注入 <code class="bg-white px-2 py-1 rounded">SpanishGreetingService</code> 实例。
                </p>
            </div>

            <div>
                <p class="text-gray-600 mb-4">如果你想明确指定使用 <code class="bg-gray-100 px-2 py-1 rounded">EnglishGreetingService</code>，可以在注入时使用 @Qualifier 注解：</p>
                
                <div class="code-block p-6 overflow-x-auto">
                    <pre><code class="text-gray-300">@Autowired
<span class="text-yellow-400">@Qualifier("englishGreetingService")</span>
private GreetingService greetingService;</code></pre>
                </div>
            </div>
        </div>

        <!-- Best Practices -->
        <div class="bg-gradient-to-r from-purple-600 to-pink-600 rounded-2xl shadow-xl p-10 text-white">
            <h2 class="text-3xl font-bold mb-6">
                <i class="fas fa-star mr-3"></i>最佳实践
            </h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white bg-opacity-20 rounded-xl p-6 backdrop-filter backdrop-blur-lg">
                    <i class="fas fa-thumbs-up text-3xl mb-3"></i>
                    <h3 class="text-xl font-semibold mb-2">明确意图</h3>
                    <p class="text-sm opacity-90">只在真正需要默认 Bean 时使用 @Primary</p>
                </div>
                <div class="bg-white bg-opacity-20 rounded-xl p-6 backdrop-filter backdrop-blur-lg">
                    <i class="fas fa-balance-scale text-3xl mb-3"></i>
                    <h3 class="text-xl font-semibold mb-2">避免滥用</h3>
                    <p class="text-sm opacity-90">一个接口只应有一个 @Primary Bean</p>
                </div>
                <div class="bg-white bg-opacity-20 rounded-xl p-6 backdrop-filter backdrop-blur-lg">
                    <i class="fas fa-file-alt text-3xl mb-3"></i>
                    <h3 class="text-xl font-semibold mb-2">文档说明</h3>
                    <p class="text-sm opacity-90">为 @Primary Bean 添加清晰的注释说明</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        mermaid.initialize({ 
            startOnLoad: true,
            theme: 'default',
            themeVariables: {
                primaryColor: '#667eea',
                primaryTextColor: '#fff',
                primaryBorderColor: '#7c3aed',
                lineColor: '#5b21b6',
                secondaryColor: '#f093fb',
                tertiaryColor: '#4ade80'
            }
        });
    </script>
</body>
</html>